package com.xjtu.duansheli.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.duansheli.entity.OrderInfo;
import com.xjtu.duansheli.entity.ProductCommentsInfo;
import com.xjtu.duansheli.exception.MyException;
import com.xjtu.duansheli.mapper.OrderInfoMapper;
import com.xjtu.duansheli.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Override
    public List<OrderInfo> getAllOrder(int current, int size) {
        Page<OrderInfo> page = new Page<>(current, size);
        orderInfoMapper.selectPage(page, null);
        System.out.println(page.getTotal());
        return page.getRecords();
    }

    @Override
    public OrderInfo getOrderByid(Long oid) {
        OrderInfo orderInfo = orderInfoMapper.selectById(oid);
        if (orderInfo == null) {
            throw new MyException(500, "不存在该交易！");
        }
        return orderInfo;
    }

    @Override
    public int addOrder(OrderInfo order) {
        try {
            return orderInfoMapper.insert(order);
        } catch (Exception e) {
            throw new MyException(500, "添加交易失败！");
        }
    }

    @Override
    public int updateOrder(OrderInfo order) {
        try {
            return orderInfoMapper.updateById(order);
        } catch (Exception e) {
            throw new MyException(500, "修改交易失败！");
        }
    }

    @Override
    public int deleteOrder(Long oid) {
        try {
            return orderInfoMapper.deleteById(oid);
        } catch (Exception e) {
            throw new MyException(500, "删除交易失败！");
        }
    }

    @Override
    public List<OrderInfo> getOrderBypid(Long pid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("product_id", pid);
        List<OrderInfo> orderInfos = orderInfoMapper.selectByMap(map);
        if (orderInfos.size() == 0) {
            throw new MyException(500, "该商品目前无交易记录！");
        }
        return orderInfos;
    }

    @Override
    public List<OrderInfo> getOrderBysaleId(Long sid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("saler_id", sid);
        List<OrderInfo> orderInfos = orderInfoMapper.selectByMap(map);
        if (orderInfos.size() == 0) {
            throw new MyException(500, "该卖家目前无交易记录！");
        }
        return orderInfos;
    }

    @Override
    public List<OrderInfo> getOrderBybuyId(Long bid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("buyer_id", bid);
        List<OrderInfo> orderInfos = orderInfoMapper.selectByMap(map);
        if (orderInfos.size() == 0) {
            throw new MyException(500, "该买家目前无交易记录！");
        }
        return orderInfos;
    }

    @Override
    public List<OrderInfo> getOrderByendTime() {
        List<OrderInfo> orderInfos = orderInfoMapper.selectList(new QueryWrapper<OrderInfo>()
                .orderByDesc("end_time")
        );
        return orderInfos;
    }

    @Override
    public int getTotal() {
        return orderInfoMapper.selectCount(null);
    }
}
